<template>
	<view>
		<!-- h5项目下 获取个人位置(选择提货点) -->
		<web-view
			src="https://apis.map.qq.com/tools/locpicker?search=1&type=1&key=J44BZ-YDZEJ-H3XF3-KCMIT-IODZQ-63F3K&referer=myapp">
		</web-view>
	</view>
</template>

<script>
	import {
		getProvinces
	} from "@/utils/tools.js" // 引入工具库
	export default {
		data() {
			return {

			}
		},
		onShow() {
			this.init();
		},
		methods: {
			init() {
				// #ifdef H5
				window.addEventListener('message', event => {
					// 接收位置信息，用户选择确认位置点后选点组件会触发该事件，回传用户的位置信息
					var loc = event.data;
					// console.log(loc);
					if (loc && loc.module == 'locationPicker') {
						//防止其他应用也会向该页面post信息，需判断module是否为'locationPicker'
						if (loc.poiname == '我的位置') {
							this.turnAdrr(loc.latlng.lng, loc.latlng.lat);
						} else {
							// 获取地址中的省市区
							let location = getProvinces(loc.poiaddress);

							location.poiaddress = loc.poiaddress; // 地区
							location.poiname = loc.poiname; // 地址详情
							location.latlng = loc.latlng; // 位置坐标

							this.goBack(location);
						}
					}
				}, false);
				// #endif
			},
			// 高德地图转地址
			async turnAdrr(longs, lat) {
				let that = this;
				// 高德逆地理变码 https://lbs.amap.com/api/webservice/guide/api/georegeo/
				let _key = 'a3a17138a4ab64bf41fa56fce54b75a8'; //高德API key类型：web服务
				let data = {
					key: _key,
					location: `${longs},${lat}`,
					output: 'JSON'
				}
				uni.request({
					method: 'GET',
					url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
					data: data,
					success: async (res) => {
						console.log(res);
						// 获取地址中的省市区
						let location = getProvinces(res.data.regeocode.formatted_address);

						location.poiaddress = res.data.regeocode.formatted_address; // 地区
						location.poiname = ''; // 地址详情
						location.latlng = {
							lng: longs,
							lat: lat
						}; // 位置坐标

						this.goBack(location);
					},
					fail: r => {
						console.log(r);
					}
				});
			},
			// 返回上一页
			goBack(location) {
				let pages = getCurrentPages();
				console.log('pages===>', pages)
				// let prePage = pages[pages.length - 2]; // 上一页页面
				// prePage.$vm.getLocation(location);
				// this.Jump({ url: '/pages/mine/address/editor_address/editor_address' })
				// this.Jump(-1)
			}
		}
	}
</script>

<style lang="scss">

</style>